package com.icygen.eclipsetools.project;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.io.FileOutputStream;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;

import com.icygen.core.svn.Repository;
import org.tigris.subversion.svnclientadapter.SVNClientException;

import com.icygen.eclipsetools.resources.IModule;
import com.icygen.eclipsetools.resources.ModuleBase;

import java.io.File;
import java.io.IOException;

import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.filter.Filters;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathFactory;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.output.XMLOutputter;
import org.jdom2.output.Format;

public class IcygenModuleSupport {

	public static final List<String> coreModules = Arrays.asList(
				"default", 
				"users", 
				"navigation"
			);
	
	
	public static ArrayList<String> getModules()
	{
		ArrayList<String> modules = new ArrayList<String>();
		
	    try {
		    Repository modulesRepository = new Repository(Repository.IcygenModulesUrl, "guest", "");
		    modules = modulesRepository.getList("");
	    } catch(SVNClientException e) {
			System.out.println(e.getMessage());
		}
	    
	    return modules;
	}
	
	public static IModule installModule(IProject project, String moduleName) 
	{	
        Assert.isNotNull(moduleName);
        Assert.isTrue(moduleName.trim().length() > 0);
 
        Repository modulesRepository = new Repository(Repository.IcygenModulesUrl, "guest", ""); 
        modulesRepository.doExport(moduleName, project.getLocation().append("htdocs/application/modules/" + moduleName).toString());
        
        /*
         * Add to installed.xml
         */
        File fileInstalledXml = new File(project.getLocation().append("htdocs/application/configs/installed.xml").toString());
        if(!fileInstalledXml.exists()) {
			System.out.println("XML File Not Exists");
		}
        
        SAXBuilder builder = new SAXBuilder();
        
        
        Document document = null;
        try {
    		document = (Document) builder.build(fileInstalledXml);
        } catch (IOException io) {
  		  	System.out.println(io.getMessage());
  	  	} catch (JDOMException jdomex) {
  	  		System.out.println(jdomex.getMessage());
  	  	}	
    		
    		
    	
		Namespace namespace = Namespace.getNamespace("zf", "http://framework.zend.com/xml/zend-config-xml/1.0/");
		XPathFactory xpfac = XPathFactory.instance();
		
		
		
		/*
		XPathExpression<Element> xp = xpfac.compile("/zend-config/modules/active/*", Filters.element(), null, namespace);
		List<Element> listNodes = xp.evaluate(document);
		
		for (Element node : listNodes) {
			if(node.getName() == moduleName) {
				
			}
 
			System.out.println(node.toString() + "\n");
		}
		 */
	 
		XPathExpression<Element> xpActive = xpfac.compile("//modules/active", Filters.element(), null, namespace);
		Element neActive = xpActive.evaluateFirst(document);
		
		XPathExpression<Element> xpInstalled = xpfac.compile("//modules/installed", Filters.element(), null, namespace);
		Element neInstalled = xpInstalled.evaluateFirst(document);
		
		if(neInstalled.getChild(moduleName) != null) {
			neInstalled.removeChild(moduleName);
		}
		
		if(neActive.getChild(moduleName) == null) {
			Element neApplicationPath = new Element("const", namespace);
			neApplicationPath.setAttribute("name", "APPLICATION_PATH", namespace);
			
			Element neModule = new Element(moduleName);
			neModule.addContent(neApplicationPath);
			neModule.addContent("/modules/" + moduleName + "/controllers");
			
			neActive.addContent(neModule);
		}	
    
		
		try {
			XMLOutputter output=new XMLOutputter(Format.getPrettyFormat());
		    output.output(document, new FileOutputStream(fileInstalledXml));
		}
		catch(IOException e) {
			System.out.println(e.getMessage());
		}
		
		
        IModule module = createModule(project, moduleName);
        
        return module;
    }
	
	private static IModule createModule(IProject project, String moduleName) {
        return new ModuleBase(moduleName);
    }
	
}
